﻿@using Flagrum.Web.Persistence.Entities
@using Index = Flagrum.Web.Features.AssetExplorer.Index
@using Flagrum.Web.Features.AssetExplorer.Data
@using Flagrum.Core.Utilities.Types
@inject FlagrumDbContext Context
@inject IStringLocalizer<Index> Localizer
@inject IStringLocalizer<Preview> PreviewLocalizer
@inject ProfileService Profile

@if (Profile.Current.Type == LuminousGame.FFXV)
{
    switch (Item?.Type)
    {
        case ExplorerItemType.Texture:
            <TexturePreview Item="@Item"/>
            break;
        case ExplorerItemType.Material:
            <MaterialPreview Item="@Item"/>
            break;
        case ExplorerItemType.Xml:
            <XmlPreview Item="@Item"/>
            break;
        case ExplorerItemType.Text:
            <TextPreview Item="@Item"/>
            break;
        case ExplorerItemType.Model:
            <ModelPreview Item="@Item"/>
            break;
        default:
            <div class="flex-grow flex flex-row items-center">
                <div class="mx-auto">
                    <strong class="text-grey-500">
                        @PreviewLocalizer["NoPreview"]
                    </strong>
                </div>
            </div>
            break;
    }
}
else
{
    switch (Item?.Type)
    {
        case ExplorerItemType.Texture:
            <TexturePreview Item="@Item"/>
            break;
        case ExplorerItemType.Material:
            <MaterialPreview Item="@Item"/>
            break;
        case ExplorerItemType.Xml:
            <XmlPreview Item="@Item"/>
            break;
        case ExplorerItemType.Text:
            <TextPreview Item="@Item"/>
            break;
        case ExplorerItemType.Model:
            <ModelPreview Item="@Item"/>
            break;
        default:
            <div class="flex-grow flex flex-row items-center">
                <div class="mx-auto">
                    <strong class="text-grey-500">
                        @PreviewLocalizer["NoPreview"]
                    </strong>
                </div>
            </div>
            break;
    }
}

@code
{
    [CascadingParameter(Name = "AssetExplorer")]
    public AssetExplorer AssetExplorer { get; set; }

    private IAssetExplorerNode _item;

    public IAssetExplorerNode Item
    {
        get => _item;
        set
        {
            if (_item != value)
            {
                _item = value;
                EarcRelativePath = Context.GetArchiveRelativeLocationByUri(Item.Path);
                var relativePath = EarcRelativePath.Contains('\\')
                    ? '\\' + EarcRelativePath[..EarcRelativePath.LastIndexOf('\\')]
                    : "";
                EarcUri = $@"{Profile.GameDataDirectory}{relativePath}";
            }
        }
    }

    public string EarcRelativePath { get; set; }
    public string EarcUri { get; set; }

    public void SetItem(IAssetExplorerNode node)
    {
        Item = node;
        AssetExplorer.CallStateHasChanged();
    }
}